package com.sigmazero13.bgjeeves;

import java.util.List;

import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.sigmazero13.bgjeeves.RenameAddDialog.OnRenameAddListener;

public abstract class RenamableInfoList extends ListActivity {

	public static final int DIALOG_RENAME = 200;
	private static final String DIALOG_NAME_ARG = "origArgs";
	
	private ListAdapter adapter;
	private RenameAddDialog renameDialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info_list);
		updateList();
		
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
	}
	
	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		if (id != DIALOG_RENAME) return null;
		
		renameDialog = new RenameAddDialog(this, getRenameDialogTitle(), RenameAddDialog.DIALOG_MODE_RENAME, getDialogRenameAddListener());
		
		return renameDialog;
	}
	
	@Override
	protected void onPrepareDialog(int id, Dialog d, Bundle args) {
		if (id != DIALOG_RENAME) return;
		
		String origName = args.getString(DIALOG_NAME_ARG);
		((RenameAddDialog)d).setDefaultText(origName);
	}
	
	protected void openRenameDialog(String origInfo) {
		Bundle args = new Bundle();
		args.putString(DIALOG_NAME_ARG, origInfo);
		showDialog(DIALOG_RENAME, args);
	}
	
	protected void updateList() {
		List<String> infos = getList();
		adapter = new ArrayAdapter<String>(this, R.layout.list_item, infos);
		this.setListAdapter(adapter);
	}
	
	protected abstract String getRenameDialogTitle();
	
	protected abstract OnRenameAddListener getDialogRenameAddListener();
	
	protected abstract List<String> getList();
}
